package com.alks.function.controller.style;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.entity.excel.BusMtlDataSheet;
import com.alks.entity.data.entity.imes.*;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.SdBomPartNew2Rep;
import com.alks.function.data.dto.chengKong.assort.YfBomPartNew2Dto;
import com.alks.function.data.request.quotation.PcStyleCraftRequest;
import com.alks.function.data.request.quotation.bom.BomDataCopyReq;
import com.alks.function.data.request.quotation.bom.BusinessMtlReq;
import com.alks.function.data.request.stock.SdMtlNewReq;
import com.alks.function.mapper.SdStyleMapper;
import com.alks.function.service.SdStyleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:hxl
 * @create: 2024-01-10 11:12
 * @Description: 型体资料取数
 */

@ApiRestController("/sdStyle")
@Api(tags = {"技术部-工厂技转作业","技术部-型体配色Bom建档","技术部-业务材料建档","技术部-web端"})
public class SdStyleController {
    @Autowired
    private SdStyleService sdStyleService;
    //PcStyleCraftRep
    @PostMapping("/getUnProcessStyle")
    @ApiOperation(value = "型体资料取数",tags = "技术部-工厂技转作业")
    @WebLogger
    public ResponseInfo getUnProcessStyle(@RequestBody PcStyleCraftRequest rep,
                                          @RequestParam @ApiParam(value = "1：未取数型体配色、2：选择型体 3:型体工艺资料检核查询 4：资料取数回显") String choose) throws InterruptedException {
        return sdStyleService.getUnProcessStyle(rep,choose);
    }

    @PostMapping("deleteAllProcess")
    @ApiOperation(value = "删除",tags = "技术部-工厂技转作业")
    @WebLogger
    public ResponseInfo deleteAllProcess(@RequestBody PcStyleCraftRequest rep){
        return sdStyleService.deleteAllProcess(rep);
    }

    //型体生产工艺
    //取资料
    @PostMapping("/insertProcess")
    @ApiOperation(value = "取资料",tags = "技术部-工厂技转作业")
    @WebLogger
    public ResponseInfo insertProcess(@RequestBody PcStyleCraftRequest rep){
        return sdStyleService.insertProcess(rep);
    }

    //型体生产工艺查询

    @PostMapping("/getStyleProductionList")
    @ApiOperation(value = "型体生产工艺查询",tags = "技术部-工厂技转作业")
    @WebLogger
    public ResponseInfo getStyleProductionList(@RequestBody PcStyleCraftRequest request,
                                               @RequestParam(required = false) @ApiParam(value = "1:有设定生产工艺的部位") String flag){
        return sdStyleService.getStyleProductionList(request,flag);
    }

    @PostMapping("/downloadStyleProcess")
    @ApiOperation(value = "型体生产工艺下载",tags = "技术部-工厂技转作业")
    @WebLogger
    public void downloadStyleProcess(HttpServletResponse response,@RequestBody List<PcStyleCraftRequest> list) throws IOException {
        sdStyleService.downloadStyleProcess(response,list);
    }

    //型体生产出工艺添加删除修改
    @PostMapping("/optionStyleProcess")
    @ApiOperation(value = "型体生产工艺添加删除修改",tags = "技术部-工厂技转作业")
    @WebLogger
    public ResponseInfo optionStyleProcess(@RequestBody StyleProductionProcess req,
                                           @RequestParam @ApiParam(value = "1: 添加 添加时前端校验序号是否已存在且序号不能为空2：删除 3：修改") String choose,
                                           @RequestParam String style,
                                           @RequestParam String styleColor){
        return sdStyleService.optionStyleProcess(req,choose,style,styleColor);
    }

    @WebLogger
    @PostMapping("/updatePcStyleProcess")
    @ApiOperation(value = "修改",tags = "技术部-工厂技转作业")
    public ResponseInfo updatePcStyleProcess(@RequestBody List<StyleProductionProcess> list){
        return sdStyleService.updatePcStyleProcess(list);
    }

    //复制
    @GetMapping("copyStyleProcess")
    @ApiOperation(value = "复制",tags = "技术部-工厂技转作业")
    @WebLogger
    public ResponseInfo copyStyleProcess(@RequestParam @ApiParam(value = "当前型体") String style1,
                                         @RequestParam @ApiParam(value = "复制型体") String style2,
                                         @RequestParam @ApiParam(value = "当前型体颜色") String color1,
                                         @RequestParam @ApiParam(value = "复制型体颜色") String color2){
        return sdStyleService.copyStyleProcess(style1,style2,color1,color2);
    }

    //工艺辅助生成 裁次辅助生成

    @WebLogger
    @PostMapping("/creatCutAndProcess")
    @ApiOperation(value = "工艺辅助生成 裁次辅助生成",tags = "技术部-工厂技转作业")
    public ResponseInfo creatCutAndProcess(@RequestBody PcStyleCraftRequest req,
                                           @RequestParam @ApiParam(value = "1：工艺辅助生成 2: 裁次辅助生成") String choose){
        return sdStyleService.creatCutAndProcess(req,choose);
    }

    //更新部位差异
    @WebLogger
    @PostMapping("/renewalSiteDifferenceList")
    @ApiOperation(value = "更新部位差异查询",tags = "技术部-工厂技转作业")
    public ResponseInfo renewalSiteDifferenceList(@RequestParam String style,@RequestParam String color){
        return sdStyleService.renewalSiteDifferenceList(style,color);
    }

    @WebLogger
    @PostMapping("/renewalSiteDifference")
    @ApiOperation(value = "更新部位差异修改、删除",tags = "技术部-工厂技转作业")
    public ResponseInfo renewalSiteDifference(@RequestBody List<PcDiffPart> req,
                                              @RequestParam @ApiParam(value = "1：修改 2：删除") String choose){
        return sdStyleService.renewalSiteDifference(req,choose);
    }
    //工艺更新记录
    @WebLogger
    @PostMapping("/processUpdateRecord")
    @ApiOperation(value = "工艺更新记录",tags = "技术部-工厂技转作业")
    public ResponseInfo processUpdateRecord(@RequestParam String style,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        return sdStyleService.processUpdateRecord(style,pageNum,pageSize);
    }
    //裁次、入库部门、工艺回显
    @WebLogger
    @GetMapping("/returnCutDeptList")
    @ApiOperation(value = "回显",tags = "技术部-工厂技转作业")
    public ResponseInfo returnCutDeptList(){
        return sdStyleService.returnCutDeptList();
    }
    //型体工艺资料检核

    @WebLogger
    @PostMapping("/checkProcessRecord")
    @ApiOperation(value = "检核和取消检核",tags = "技术部-工厂技转作业")
    public ResponseInfo checkProcessRecord(@RequestBody List<PcStyleCraftRequest> req,
                                           @RequestParam @ApiParam(value = "1:检核 2：取消检核") String check){
        return sdStyleService.checkProcessRecord(req,check);
    }

    @WebLogger
    @PostMapping("/getSdBomFitSubNew2")
    @ApiOperation(value = "贴合子材料明细",tags = "技术部-型体配色Bom建档")
    public ResponseInfo getSdBomFitSubNew2(@RequestBody YfBomPartNew2Dto req){
        return sdStyleService.getSdBomFitSubNew2(req);
    }

    //贴合子材分解 Laminated wood decomposition
    @WebLogger
    @PostMapping("/laminatedWoodDecomposition")
    @ApiOperation(value = "贴合子材分解",tags = "技术部-型体配色Bom建档")
    public ResponseInfo laminatedWoodDecomposition(@RequestBody @ApiParam(value = "型体 型体颜色 部位 分段 材料颜色 材料名称 单位 规格") List<YfBomPartNew2Dto> list){
        return sdStyleService.laminatedWoodDecomposition(list);
    }
    //贴合分拆确认 Fit split confirmation
    @WebLogger
    @PostMapping("/fitSplitConfirmation")
    @ApiOperation(value = "贴合子材确认",tags = "技术部-型体配色Bom建档")
    public ResponseInfo fitSplitConfirmation(@RequestBody SdBomFitSubNewReq req){
        return sdStyleService.fitSplitConfirmation(req);
    }
    @WebLogger
    @PostMapping("/updateFitSplitConfirmation")
    @ApiOperation(value = "贴合子材修改",tags = "技术部-型体配色Bom建档")
    public ResponseInfo updateFitSplitConfirmation(@RequestBody SdBomFitSubNew2 req){
        return sdStyleService.updateFitSplitConfirmation(req);
    }
    //分段部位建档 Sectional section filing

    @WebLogger
    @PostMapping("/sectionalSectionFiling")
    @ApiOperation(value = "分段部位建档",tags = "技术部-型体配色Bom建档")
    public ResponseInfo sectionalSectionFiling(@RequestBody YfBomPartNew2Dto req,
                                               @RequestParam @ApiParam(value = "分段层数") Integer segment){
        return sdStyleService.sectionalSectionFiling(req,segment);
    }

    //材料颜色查询
    @WebLogger
    @PostMapping("/getMtlColor")
    @ApiOperation(value = "材料颜色查询",tags = "技术部-型体配色Bom建档")
    public ResponseInfo getMtlColor(@RequestParam(required = false) String colorName,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        return sdStyleService.getMtlColor(colorName,pageNum,pageSize);
    }

    //生成物料编码 如果贴合子材料有数据 需要先贴合分拆确认 Generate material code

    @WebLogger
    @PostMapping("/generateMaterialCode")
    @ApiOperation(value = "生成物料编码",tags = "技术部-型体配色Bom建档")
    public ResponseInfo generateMaterialCode(@RequestBody List<YfBomPartNew2Dto> list){
        return sdStyleService.generateMaterialCode(list);
    }

    //置空物料编码
    @WebLogger
    @PostMapping("/setNullMaterialCode")
    @ApiOperation(value = "置空物料编码",tags = "技术部-型体配色Bom建档")
    public ResponseInfo setNullMaterialCode(@RequestBody YfBomPartNew2Dto req){
        return sdStyleService.setNullMaterialCode(req);
    }

    //分类辅助生成
    @WebLogger
    @PostMapping("/createTypeNo")
    @ApiOperation(value = "分类辅助生成",tags = "技术部-型体配色Bom建档")
    public ResponseInfo createTypeNo(@RequestBody List<YfBomPartNew2Dto> req){
        return sdStyleService.createTypeNo(req);
    }

    //分类关键词定义 Classification keyword definition
    @WebLogger
    @PostMapping("/classificationKeyList")
    @ApiOperation(value = "分类关键词定义查询",tags = "技术部-型体配色Bom建档")
    public ResponseInfo classificationKeyList(@RequestBody SdMtlTypeKey req){
        return sdStyleService.classificationKeyList(req);
    }

    //分类关键增加
    @WebLogger
    @PostMapping("/addClassificationKey")
    @ApiOperation(value = "分类关键词增加",tags = "技术部-型体配色Bom建档")
    public ResponseInfo addClassificationKey(@RequestBody SdMtlTypeKey req){
        return sdStyleService.addClassificationKey(req);
    }
    //复制当前材料到勾选部位

    @PostMapping("/copyCurrTo")
    @ApiOperation(value = "复制当前材料到勾选部位",tags = "技术部-型体配色Bom建档")
    @WebLogger
    public ResponseInfo copyCurrTo(@RequestBody SdBomPartNew2Rep req){
        return sdStyleService.copyCurrTo(req);
    }

    //bom资料复制
    @WebLogger
    @PostMapping("/bomDataReplication")
    @ApiOperation(value = "bom资料复制",tags = "技术部-型体配色Bom建档")
    public ResponseInfo bomDataReplication(@RequestBody BomDataCopyReq req){
        return sdStyleService.bomDataReplication(req);
    }

    //技术部-业务材料建档
    //生成业务材料

    @WebLogger
    @PostMapping("/generateBMtl")
    @ApiOperation(value = "生成业务材料",tags = "技术部-业务材料建档")
    public ResponseInfo generateBMtl(@RequestBody BusinessMtlReq req){
        return sdStyleService.generateBMtl(req);
    }

    //贴合材料处理 SD_MTL_SUB_NEW
    @WebLogger
    @GetMapping("/getSdMtlSubNew")
    @ApiOperation(value = "贴合材料处理 子材料明细",tags = "技术部-业务材料建档")
    public ResponseInfo getSdMtlSubNew(@RequestParam String mtlNo){
        return sdStyleService.getSdMtlSubNew(mtlNo);
    }

    @WebLogger
    @GetMapping("/getSdMtlSubFit")
    @ApiOperation(value = "子材料明细",tags = "技术部-业务材料建档")
    public ResponseInfo getSdMtlSubFit(@RequestParam String mtlNo){
        return sdStyleService.getSdMtlSubFit(mtlNo);
    }
    //材料查询
    @WebLogger
    @PostMapping("getSdBomMtl")
    @ApiOperation(value = "材料查询",tags = "技术部-业务材料建档")
    public ResponseInfo getSdBomMtl(@RequestBody SdMtlNewReq req){
        return sdStyleService.getSdBomMtl(req);
    }

    //贴合材料处理
    @PostMapping("/subFitOption")
    @WebLogger
    @ApiOperation(value = "贴合材料处理 确认",tags = "技术部-业务材料建档")
    public ResponseInfo subFitOption(@RequestBody List<SdBomFitSub> list){
        return sdStyleService.subFitOption(list);
    }

    @GetMapping("/getColorWithCode")
    @WebLogger
    @ApiOperation(value = "颜色查询",tags = "技术部-业务材料建档")
    public ResponseInfo getColorWithCode(@RequestParam(required = false) String color,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        return sdStyleService.getColorWithCode(color,pageNum,pageSize);
    }

    @PostMapping("/busMtlDelete")
    @WebLogger
    @ApiOperation(value = "删除",tags = "技术部-业务材料建档")
    public ResponseInfo busMtlDelete(@RequestParam String mtlNo){
        return sdStyleService.busMtlDelete(mtlNo);
    }

    @Autowired
    private SdStyleMapper sdStyleMapper;
    @ApiOperation(value = "导出",tags = "技术部-业务材料建档")
    @PostMapping("/busMtlDownload")
    public void download(HttpServletResponse response,@RequestBody SdMtlNewReq req) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        List<BusMtlDataSheet> list = sdStyleMapper.sdBomMtlList(req);
        InputStream inputStream = new ClassPathResource("templates/业务材料汇总表.xls").getInputStream();
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String s = new SimpleDateFormat("yyyyMMhhmmss").format(new Date());
        String string = "业务材料汇总表_"+s;
        String fileName = URLEncoder.encode(string, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).excelType(ExcelTypeEnum.XLS).withTemplate(inputStream).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(list,writeSheet);
            Map<String, String> map = new HashMap<>();
            excelWriter.fill(map, writeSheet);
        }
    }
    @GetMapping("/getParamSet")
    @ApiOperation(value = "参数设定查询",tags = "技术部-web端")
    @WebLogger
    public ResponseInfo getParamSet(@RequestParam @ApiParam(value = "type") String type,
                                    @RequestParam @ApiParam(value = "name")String name,
                                    @RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize){
        return sdStyleService.getParamSet(type,name,pageNum,pageSize);
    }

    //参数添加
    @PostMapping("/optionParam")
    @WebLogger
    @ApiOperation(value = "新增、删除",tags = "技术部-web端")
    public ResponseInfo optionParam(@RequestBody ParamSettingResp req,@RequestParam @ApiParam(value = "1:新增 2：删除") String choose){
        return sdStyleService.optionParam(req,choose);
    }
    //任务建档

    @GetMapping("/getTaskFiling")
    @WebLogger
    @ApiOperation(value = "任务建档查询",tags = "技术部-web端")
    public ResponseInfo getTaskFiling(@RequestParam(required = false) @ApiParam(value = "品牌") String custNo,
                                      @RequestParam(required = false) @ApiParam(value = "型体") String styleNo,
                                      @RequestParam(required = false) @ApiParam(value = "状态 1:已下单 2：未下单 3：已隐藏 4：未隐藏") String taskState,
                                      @RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize){
        return sdStyleService.getTaskFiling(custNo,styleNo,taskState,pageNum,pageSize);
    }

    @PostMapping("/optionTask")
    @WebLogger
    @ApiOperation(value = "勾选隐藏、取消隐藏",tags = "技术部-web端")
    public ResponseInfo optionTask(@RequestBody List<TaskFillingResp> req, @RequestParam @ApiParam("1:勾选隐藏2：取消隐藏") String choose){
        return sdStyleService.optionTask(req,choose);
    }

    //任务明细
    @GetMapping("/getTaskFilingDetail")
    @WebLogger
    @ApiOperation(value = "任务明细",tags = "技术部-web端")
    public ResponseInfo getTaskFilingDetail(@RequestParam String styleNo,@RequestParam String styleColor){
        return sdStyleService.getTaskFilingDetail(styleNo,styleColor);
    }

    //勾选打任务单打印
    @PostMapping("/optionTaskPrintFlag")
    @WebLogger
    @ApiOperation(value = "勾选打任务单打印",tags = "技术部-web端")
    public ResponseInfo optionTaskPrintFlag(@RequestBody List<TrailTaskHead> request){
        return sdStyleService.optionTaskPrintFlag(request);
    }


    //更新
    @PostMapping("/updateTaskStateQty")
    @WebLogger
    @ApiOperation(value = "更新",tags = "技术部-web端")
    public ResponseInfo updateTaskStateQty(@RequestBody TaskFilingDetailResp req){
        return sdStyleService.updateTaskStateQty(req);
    }



    //勾选重做

    @PostMapping({"/tree/list"})
    @ApiOperation(value = "技转进度树",tags = "技术部-web端")
    @WebLogger
    public List<JiZhuanJinDuTree> treeList(JiZhuanJinDuSearchObj searchObj){
        return sdStyleService.treeList(searchObj);
    }

    //技转工艺查询
    @PostMapping({"/queryHeadList"})
    @ApiOperation(value = "技转进度详情查询",tags = "技术部-web端")
    @WebLogger
    public ResponseInfo queryHeadList(JiZhuanJinDuSearchObj searchObj) {
        return sdStyleService.queryHeadList(searchObj);
    }


    //建任务
    @PostMapping({"/creatTask"})
    @ApiOperation(value = "建任务",tags = "技术部-web端")
    @WebLogger
    public ResponseInfo creatTask(@RequestBody TaskFillingResp req){
        return sdStyleService.creatTask(req);
    }

    //技转进度导出
    @PostMapping({"/exportHead"})
    @ApiOperation(value = "技转进度导出",tags = "技术部-web端")
    @WebLogger
    public void exportHead(HttpServletResponse response,JiZhuanJinDuSearchObj searchObj) throws IOException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<JiZhuanJinDu> list = sdStyleMapper.queryJiZhuanJinDu(searchObj);
        list.forEach(x->x.setSysTime(format.format(x.getSysDate())));
        ExcelUtils.creatByTemplate(response,"web技转进度模板.xls","JiZhuanJinDu"+ DateUtils.dateTimeNow(),list);
    }

    @PostMapping("/tree/problem/list")
    @ApiOperation(value = "问题状况查询",tags = "技术部-web端")
    @WebLogger
    public List<JiZhuanJinDuTree> proplemList(JiZhuanJinDuSearchObj searchObj){
        return sdStyleService.proplemList(searchObj);
    }

    @PostMapping({"/queryProblemHeadList"})
    @ApiOperation(value = "问题明细",tags = "技术部-web端")
    @WebLogger
    public ResponseInfo queryProblemHeadList(JiZhuanJinDuSearchObj searchObj) {
        return sdStyleService.queryProblemHeadList(searchObj);
    }

    @PostMapping({"/exportProblemHead"})
    @ApiOperation(value = "问题明细导出",tags = "技术部-web端")
    @WebLogger
    public void exportProblemHead(HttpServletResponse response,JiZhuanJinDuSearchObj searchObj) throws IOException {
        searchObj.setCompanyId(UserIdThread.get().getCompanyId());
        List<TaskProblemSolve> list = sdStyleMapper.queryTaskProblemSolve(searchObj);
        for (TaskProblemSolve solve : list) {
            if (solve.getProblemPath() !=null && !"".equals(solve.getProblemPath())) solve.setProblemURL(new URL(solve.getProblemPath()));
            if (solve.getSolvePath() !=null && !"".equals(solve.getSolvePath())) solve.setSolveURL(new URL(solve.getSolvePath()));
        }
        ExcelUtils.creatByTemplateWithHandler(response,"web问题明细模板.xls","JiZhuanJinDuWenTi"+ DateUtils.dateTimeNow(),list);
    }
}
